package com.hnucm.chapter3_1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Scroller;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ViewActivity extends AppCompatActivity {

    private final static String TAG="ViewActivity";

    private TextView textView2;
    private TextView textView1;
    private TextView textView;
    private TextView textView3;
    private Button moveBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        textView=findViewById(R.id.textView);
        textView1=findViewById(R.id.textView1);
        textView2=findViewById(R.id.textView2);
        textView3=findViewById(R.id.textView3);
        moveBtn=findViewById(R.id.animBtn);


        Activity activity;
        //1.view的继承结构
        View view;
        Button button;
        ConstraintLayout constraintLayout;

        //2.view的位置参数
        //都为0
        Log.e(TAG, "onCreate: " +
                "top="+textView.getTop()+"  " +
                "left="+textView.getLeft()+"   " +
                "right="+textView.getLeft()+"  " +
                "bottom="+textView.getBottom());

        Log.e(TAG, "onCreate: " +
                "top="+textView1.getTop()+"  " +
                "left="+textView1.getLeft()+"   " +
                "right="+textView1.getLeft()+"  " +
                "bottom="+textView1.getBottom());

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //绝对滑动
                textView1.scrollTo(-100,0);
                Log.e(TAG, "onClick: textview1 mScrollX="+textView1.getScrollX() );
                //相对滑动
                textView.scrollBy(-100,0);
                Log.e(TAG, "onClick: textview mScrollX="+textView.getScrollX() );
            }
        });


        //使用原生动画实现滑动
        Animation animation= AnimationUtils.loadAnimation(this,R.anim.a1_translate);

        findViewById(R.id.textView1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView1.startAnimation(animation);
            }
        });
//        使用属性动画实现滑动
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator.ofFloat(textView2,"translationX",0,100).
                        setDuration(1000).start();
            }
        });

//        3.改变布局参数实现滑动
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewGroup.MarginLayoutParams params=(ViewGroup.MarginLayoutParams)textView3.getLayoutParams();
                params.leftMargin+=100;
                textView3.requestLayout();
            }
        });



        //
        moveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ViewActivity.this,MoveActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        TextView textView=findViewById(R.id.textView);
        Log.e(TAG, "onStart: " +
                "top="+textView.getTop()+"  " +
                "left="+textView.getLeft()+"   " +
                "right="+textView.getLeft()+"  " +
                "bottom="+textView.getBottom());

        TextView textView1=findViewById(R.id.textView1);
        Log.e(TAG, "onStart: " +
                "top="+textView1.getTop()+"  " +
                "left="+textView1.getLeft()+"   " +
                "right="+textView1.getLeft()+"  " +
                "bottom="+textView1.getBottom());
    }

    @Override
    protected void onResume() {
        super.onResume();
        TextView textView=findViewById(R.id.textView);
        Log.e(TAG, "onResume: " +
                "top="+textView.getTop()+"  " +
                "left="+textView.getLeft()+"   " +
                "right="+textView.getLeft()+"  " +
                "bottom="+textView.getBottom());

        TextView textView1=findViewById(R.id.textView1);
        Log.e(TAG, "onResume: " +
                "top="+textView1.getTop()+"  " +
                "left="+textView1.getLeft()+"   " +
                "right="+textView1.getLeft()+"  " +
                "bottom="+textView1.getBottom());
    }

    @Override
    protected void onStop() {
        super.onStop();
        TextView textView=findViewById(R.id.textView);
        Log.e(TAG, "onStop: " +
                "top="+textView.getTop()+"  " +
                "left="+textView.getLeft()+"   " +
                "right="+textView.getLeft()+"  " +
                "bottom="+textView.getBottom());

        TextView textView1=findViewById(R.id.textView1);
        Log.e(TAG, "onStop: " +
                "top="+textView1.getTop()+"  " +
                "left="+textView1.getLeft()+"   " +
                "right="+textView1.getLeft()+"  " +
                "bottom="+textView1.getBottom());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        TextView textView=findViewById(R.id.textView);
        Log.e(TAG, "onDestroy: " +
                "top="+textView.getTop()+"  " +
                "left="+textView.getLeft()+"   " +
                "right="+textView.getLeft()+"  " +
                "bottom="+textView.getBottom());

        TextView textView1=findViewById(R.id.textView1);
        Log.e(TAG, "onDestroy: " +
                "top="+textView1.getTop()+"  " +
                "left="+textView1.getLeft()+"   " +
                "right="+textView1.getLeft()+"  " +
                "bottom="+textView1.getBottom());
    }
}